package com.woniu.user.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.user.commons.Result;
import com.woniu.user.controller.form.*;
import com.woniu.user.model.Account;
import com.woniu.user.model.Coach;
import com.woniu.user.model.Venue;
import com.woniu.user.service.VenueService;
import com.woniu.user.service.dto.CoachInfoDto;
import com.woniu.user.service.dto.VenueDto;
import com.woniu.user.util.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 * 场馆表 前端控制器
 * </p>
 *
 * @author Silas
 * @since 2023-02-15
 */
@RestController
@RequestMapping("/venue")
@Api(tags = "场馆登录注册")
@Slf4j
public class VenueController {

    @Resource
    private VenueService venueService;

    @PostMapping("/venueLogin")
    @ApiOperation("场馆登录")
    public Result venueLogin(@RequestBody VenueLoginForm venueLoginForm, HttpServletRequest request) {
        log.info("执行登录操作{}", venueLoginForm);
        Cookie cookie = venueService.venueLogin(venueLoginForm);
        HttpSession session = request.getSession();
        session.setAttribute("token", cookie);
        return Result.success(200, "登录成功", null);
    }

    @PostMapping("/venueRegister")
    @ApiOperation("场馆注册")
    public Result venueRegister(@RequestBody VenueRegisterForm venueRegisterForm) throws MessagingException {
        log.info("执行注册操作{}", venueRegisterForm);
        venueService.venueRegister(venueRegisterForm);
        return Result.success(200, "注册成功", null);
    }

    @SneakyThrows
    @GetMapping("/venueActive/{id}")
    @ApiOperation("场馆账号激活")
    public Result venueActive(@PathVariable Integer id, HttpServletResponse resp) {
        venueService.venueActive(id);
        resp.sendRedirect("/venue/activeSuccess.html");
        return Result.success(200, "激活成功", null);
    }

    @PostMapping("/updatePass")
    @ApiOperation("场馆密码修改")
    public Result updatePass(@RequestBody UpdatePassForm updatePassForm) {
        log.info("执行修改密码操作 {}", updatePassForm);
        boolean update = venueService.update(updatePassForm);
        if (update) {
            return Result.success(200, "修改成功", updatePassForm);
        }
        return Result.fail(500, "修改失败");
    }

    @GetMapping("/venue")
    @ApiOperation("场馆分页查询")
    public Result get(String condition, Integer current, Integer size) {
        log.info("执行了场馆查询操作 {}{}{}", condition, current, size);
        Page<Venue> list = venueService.query(condition, current, size);
        //Page<VenueDto> list = venueService.query(condition, current, size);
        return Result.success(200, "查询成功", list);
    }

    @GetMapping("/venues")
    @ApiOperation("场馆分页查询")
    public Result getAllVenue(String condition, Integer current, Integer size) {
        log.info("执行了场馆查询操作condition {} current{} size{}", condition, current, size);
        Page<VenueDto> list = venueService.getAllVenue(condition, current, size);
        //Page<VenueDto> list = venueService.query(condition, current, size);
        return Result.success(200, "查询成功", list);
    }


    @GetMapping("/queryVenue")
    @ApiOperation("条件查询")
    public Result queryVenue(String condition, Integer current, Integer size) {
        log.info("条件查询{}", condition);
        if ("".equals(condition) || condition == null) {
            Page<Venue> pageInfo = new Page<>(current == null ? 1 : current, size == null ? 4 : size);
            IPage<Venue> allCoach = venueService.findAll(pageInfo);
            return Result.success(200, "查询成功", allCoach);
        } else {
            Page<Venue> query = venueService.query(condition, current, size);
            return Result.success(200, "查询成功", query);
        }
    }


    @GetMapping("/testEmail")
    @ApiOperation("邮箱注册检测")
    public Result testEmail(String email) {
        log.info("执行了邮箱是否被注册操作 {}", email);
        if (email != null) {
            Venue one = venueService.getOne(new QueryWrapper<Venue>().eq("email", email));
            if (one != null) return Result.success(200, "该邮箱已被注册", null);
        }
        return null;
    }

    @PostMapping("/emailCode")
    @ApiOperation("发送邮箱验证码")
    public Result emailCode(String email) {
        log.info("执行了邮箱验证码发送操作 {}", email);
        venueService.emailCode(email);
        return Result.success(200, "发送成功", null);
    }


    @GetMapping("/receivedCode")
    @ApiOperation("收到验证码")
    public Result receivedCode(String email, String code) {
        log.info("执行了收到验证码操作 {} {}", email, code);
        venueService.receivedCode(email, code);
        return Result.success(200, "收到验证码", null);
    }

    @PostMapping("/resetPwd")
    @ApiOperation("密码重置")
    public Result resetPwd(@RequestBody VenueRegisterForm venueRegisterForm) {
        log.info("执行了密码重置操作 {}", venueRegisterForm);
        venueService.resetPwd(venueRegisterForm);
        return Result.success(200, "重置成功", null);
    }


    @PostMapping("/venueModify")
    @ApiOperation("场馆信息修改")
    public Result venueModify(@RequestBody ModifyVenueForm modifyVenueForm, HttpServletRequest request) {
        log.info("执行了修改场馆信息操作 {}", modifyVenueForm);
        Integer userId = JwtUtil.getVenueIdFromReq(request);// 获取场馆账号
        venueService.venueModify(userId, modifyVenueForm);
        return Result.success(200, "修改成功", venueService.getUserInfo(userId));
    }

    @GetMapping("/getMyVenue")
    @ApiOperation("获取场馆信息查询")
    public Result getMyVenue(HttpServletRequest request) {
        log.info("获取场馆信息 {}", request);
        Integer userId = JwtUtil.getVenueIdFromReq(request);
        log.info("userId {}", userId);
        VenueDto userInfo = venueService.getUserInfo(userId);
        return Result.success(200, "查询成功", userInfo);
    }

    @GetMapping("/query")
    public Result queryAll() {
        log.info("查询所有场馆");
        List<Venue> list = venueService.findVenue();
        return Result.success(200, "查询成功", list);
    }


}

